<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model;

/**
 * 语言配置模型
 */
class Language extends Model
{
    protected $table = 'languages';

    protected function getOptions(): array
    {
        return [
            'autoWriteTimestamp' => true,
            'jsonAssoc'          => true,
        ];
    }

    /**
     * 获取启用的语言列表
     */
    public static function getEnabledLanguages()
    {
        return self::where('is_enabled', 1)
            ->order('sort_order', 'asc')
            ->select();
    }

    /**
     * 获取默认语言
     */
    public static function getDefaultLanguage()
    {
        return self::where('is_default', 1)->find();
    }

    /**
     * 根据代码获取语言
     */
    public static function getByCode($code)
    {
        return self::where('code', $code)->find();
    }

    /**
     * 获取语言选项（用于下拉选择）
     */
    public static function getLanguageOptions()
    {
        return self::getEnabledLanguages()->column('name', 'code');
    }

    /**
     * 检查语言代码是否有效
     */
    public static function isValidLanguageCode($code)
    {
        return self::where('code', $code)
            ->where('is_enabled', 1)
            ->count() > 0;
    }
}
